package cn.tyoui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tyoui.bean.XyffBean;
import cn.tyoui.service.XyffManageService;

@Controller
public class XyffController {

	@Resource
	private XyffManageService xyffManageService;
	
	@ResponseBody
	@RequestMapping("btn_delete_xyff")
	public Map<String,Object> deleteXyff(@RequestBody Map<String,String> map){
		Map<String,Object> resultMap = new HashMap<>();
		String id = map.get("id");
		System.out.println("id : " + id);
		boolean isSuccess = false;
		if(id != null && !id.equals("")) {
			try {
				xyffManageService.deleteXyff(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			isSuccess = true;
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("change_xyff")
	public Map<String,Object> changeXyff(@RequestBody XyffBean xyff){
		System.out.println("change_xyff");
		System.out.println(xyff);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(xyff != null && xyff.getId() != 0 && xyff.getXyff()!=null && !xyff.getXyff().equals("")) {
			try {
				List<XyffBean> xyffs = xyffManageService.queryXyff(xyff);
				if(xyffs.size() == 0) {
					xyffManageService.changeXyff(xyff);
					isSuccess = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("isSuccess", isSuccess);
		resultMap.put("status", "200");
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("query_xyff")
	public Map<String,Object> queryXyff(@RequestBody XyffBean xyff){
		System.out.println("query_cfdd");
		System.out.println(xyff);
		Map<String,Object> resultMap = new HashMap<>();
		if(xyff != null) {
			try {
				List<XyffBean> xyffs = xyffManageService.queryXyff(xyff);
				System.out.println(xyffs.size());
				resultMap.put("xyffs", xyffs);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("add_xyff")
	public Map<String,Object> addXyff(@RequestBody XyffBean xyff){
		System.out.println("add_xyff");
		System.out.println(xyff);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(xyff != null) {
			try {
				List<XyffBean> xyffs = xyffManageService.queryXyff(xyff);
				System.out.println(xyffs.size());
				if(xyffs.size() == 0) {
					isSuccess = xyffManageService.addXyff(xyff);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
}
